#region Copyright (C) 1998-2009 Autumoon Lab, all rights reserved.
/* *************************************************************
 * Solution:    Autumoon Code Library - Standard Edition
 * 
 * Project:     Database Controller
 * 
 * Description: The control tool class of Option.
 * 
 * Auther:      ZeroCool.
 * 
 * Date:        March 23, 2008.
 * 
 * Copyright (C) 1998-2009 Autumoon Lab, all rights reserved.
 * ************************************************************/
#endregion

#region References
using System;
using System.Data;
using System.Data.Odbc;

using Autumoon.CodeLibrary.CommonFoundation;
#endregion

namespace Autumoon.CodeLibrary.DatabaseController
{
    public class OptionController
    {
        // Properties and fields.
        #region Properties and fields
        #endregion

        // Constructors.
        #region Constructors
        #endregion

        // Public methods.
        #region Public Methods
        /// <summary>
        /// Save current theme's name.
        /// </summary>
        /// <param name="themeName">Current theme's name.</param>
        public static void SaveTheme(string themeName)
        {
            using (OdbcConnection connection = new OdbcConnection(ConstantString.ConnectionString))
            {
                OdbcCommand command = connection.CreateCommand();
                command.CommandText = "Update " + ConstantString.OptionsTableName + " Set ThemeName = '" + themeName + "';";

                connection.Open();

                if (command.ExecuteNonQuery() != 1)
                {
                    throw new InvalidOperationException("Save theme option failed.");
                }
            }
        }

        /// <summary>
        /// Get current theme's name.
        /// </summary>
        /// <returns>Current theme's name.</returns>
        public static string GetCurrentTheme()
        {
            using (OdbcConnection connection = new OdbcConnection(ConstantString.ConnectionString))
            {
                OdbcCommand command = connection.CreateCommand();
                command.CommandText = "Select ThemeName From " + ConstantString.OptionsTableName + ";";

                connection.Open();

                OdbcDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    return reader[0].ToString();
                }

                return String.Empty;
            }
        }

        /// <summary>
        /// Check whether the user registered.
        /// </summary>
        /// <returns>Registered or not.</returns>
        public static bool CheckRegistered()
        {
            using (OdbcConnection connection = new OdbcConnection(ConstantString.ConnectionString))
            {
                OdbcCommand command = connection.CreateCommand();
                command.CommandText = "Select R From " + ConstantString.OptionsTableName + ";";

                connection.Open();

                OdbcDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    return (reader["R"].ToString() == "1");
                }

                throw new InvalidOperationException("Check registered failed.");
            }
        }

        /// <summary>
        /// Set the sign in database when user registered succeed.
        /// </summary>
        public static void SetRegistered()
        {
            using (OdbcConnection connection = new OdbcConnection(ConstantString.ConnectionString))
            {
                OdbcCommand command = connection.CreateCommand();
                command.CommandText = "Update " + ConstantString.OptionsTableName + " Set R = 1;";

                connection.Open();

                if (command.ExecuteNonQuery() != 1)
                {
                    throw new InvalidOperationException("Set register sign failed.");
                }
            }
        }
        #endregion

        // Internal methods.
        #region Internal Methods
        #endregion

        // Protected methods.
        #region Protected Methods
        #endregion

        // Private methods.
        #region Private Methods
        #endregion
    }
}